package xiaopiya.robot.handler.joke;

import xiaopiya.robot.handler.AbstractMessageHandler;

/**
 * @author Ray (ayufox@gmail.com)
 * @version 0.1
 */
public class JokeMessageHandler extends AbstractMessageHandler
{
	private IJokeService jokeService;

	public void setJokeService(IJokeService jokeService)
	{
		this.jokeService = jokeService;
	}

	/* (non-Javadoc)
	 * @see xiaopiya.robot.handler.AbstractMessageHandler#handle(java.lang.String, java.lang.String)
	 */
	protected String handle(String from, String params) throws Exception
	{
		int type = 0;
		String[] array = params.split(" ");
		for (String param : array)
		{
			if (param.startsWith("-t"))
			{
				String typeString = param.substring("-t".length(), param.length()).trim();
				try
				{
					type = Integer.parseInt(typeString);
				}
				catch (NumberFormatException t)
				{
					return "你所指定的笑话类型不存在";
				}
			}
		}
		return this.jokeService.getJoke(type);
	}

}
